package com.qz.mos.entity;


import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.UUIDGenerator;
import org.springframework.util.DigestUtils;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;

public class Md5Id extends UUIDGenerator {

    public static String encrypt3ToMD5(RestDataMD5 object1) {
        if (!"String".equals(  object1.getType())) {
            byte[] pic = object1.getPic();

            return encrypt3ToMD5( pic );
        } else {
            return encrypt3ToMD5( object1.getData() );
        }
    }

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        if (object instanceof RestDataMD5) {
            RestDataMD5 object1 = (RestDataMD5) object;
            return encrypt3ToMD5(object1);
        } else {
            return super.generate( session, object );
        }

    }

    public static String getSysYear() {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf( date.get( Calendar.YEAR ) );
        return year;
    }

    /**
     * MD5加密之方法三
     *
     * @param str 待加密字符串
     * @return 16进制加密字符串
     * @explain springboot自带MD5加密
     */
    public static String encrypt3ToMD5(String str) {
        String md5 = "";
        try {
            md5 = DigestUtils.md5DigestAsHex( str.getBytes( "utf-8" ) );
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return md5;
    }

    /**
     * MD5加密之方法三
     *
     * @param str 待加密字符串
     * @return 16进制加密字符串
     * @explain springboot自带MD5加密
     */
    public static String encrypt3ToMD5(byte[] str) {
        String md5;
        md5 = DigestUtils.md5DigestAsHex( str );
        return md5;
    }

}